package com.muyan.common.check;

import com.muyan.common.bean.Params;
import com.muyan.common.bean.Result;

import java.util.Arrays;
import java.util.List;

/**
 * 多条件检查
 * 放入多个checker，注意检查是按顺序的
 */
@SuppressWarnings("unchecked")
public class CheckHandler {

    private List<Checker> checkers;

    public CheckHandler(Checker... checkers) {
        if (checkers == null) {
            throw new IllegalArgumentException("至少需要一个检查项");
        }
        this.checkers = Arrays.asList(checkers);
    }

    public Result<Params> hand(Params params) {
        Params dealed = Params.build();
        for (Checker checker : checkers) {
            Result result = checker.check(params);
            if (!result.succ) return result;
            if (result.data != null) dealed.putAll((Params) result.getData());
        }
        return Result.succ(dealed);
    }

}
